package com.ohimy.action;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.ohimy.entity.Group;
import com.ohimy.entity.Scrap;
import com.ohimy.entity.User;
import com.ohimy.service.impl.ProfileService;
import com.ohimy.service.impl.UserService;

@Controller("profileAction")
public class ProfileAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	@Resource(name = "profileService")
	private ProfileService profileService;

	@Resource(name = "userService")
	private UserService userService;

	private User user = new User();
	private Scrap scrap = new Scrap();
	private String flag; // 0:自己查看;1:他人查看
	private List<User> friendList;
	private List<Group> groupList;
	private List<Scrap> scrapList;
	private String page_index, maxResult; // 当前页，接受的ID，总数
	private long total;

	@Override
	public String execute() throws Exception {
		if (super.getSession() == null)
			result = "/WEB-INF/site/user/noLogin.jsp";
		else {
			user = id == 0 ? userService.findUserById(getSession().getId())
					: profileService.getUserById(id);
			flag = id == user.getId() ? "owner" : "guest";
			// 得到用户的朋友列表
			friendList = profileService.getUsersFriends(user);
			// 得到用户加入的群组
			groupList = profileService.getUsersGroups(user);
			// 得到用户留言总数
			total = profileService.getScrapTotal(user);
			result = "/WEB-INF/site/profile/";
		}
		return "ohimy";
	}

	/**
	 * 留言板的分页，每页5条
	 * 
	 * @return
	 * @throws Exception
	 */
	public String page() throws Exception {
		int page_index = Integer.parseInt(this.page_index);
		int maxResult = Integer.parseInt(this.maxResult);
		scrapList = profileService.getScrapByPager(id, page_index * maxResult,
				maxResult);
		result = "/WEB-INF/site/ajax/profile_scrap_body.jsp";
		return "ohimy";
	}

	/**
	 * 发布留言,并返回最后一条信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public String saveScrap() throws Exception {
		scrap.setLeaver(userService.findUserById(getSession().getId()));
		scrap.setCreated(new Date());
		scrap.setOwner(user);
		scrapList = profileService.saveScrap(scrap);
		result = "/WEB-INF/site/ajax/profile_scrap_body.jsp";
		return "ohimy";
	}

	/* ********************************************** */
	public void setProfileService(ProfileService profileService) {
		this.profileService = profileService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public List<User> getFriendList() {
		return friendList;
	}

	public void setFriendList(List<User> friendList) {
		this.friendList = friendList;
	}

	public List<Group> getGroupList() {
		return groupList;
	}

	public void setGroupList(List<Group> groupList) {
		this.groupList = groupList;
	}

	public List<Scrap> getScrapList() {
		return scrapList;
	}

	public void setScrapList(List<Scrap> scrapList) {
		this.scrapList = scrapList;
	}

	public String getPage_index() {
		return page_index;
	}

	public void setPage_index(String page_index) {
		this.page_index = page_index;
	}

	public String getMaxResult() {
		return maxResult;
	}

	public void setMaxResult(String maxResult) {
		this.maxResult = maxResult;
	}

	public long getTotal() {
		return total;
	}

	public void setTotal(long total) {
		this.total = total;
	}

	public Scrap getScrap() {
		return scrap;
	}

	public void setScrap(Scrap scrap) {
		this.scrap = scrap;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}